package com.ruoyi.operator.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.operator.domain.OptMemberOrderDetail;
import com.ruoyi.operator.domain.OptShopMember;
import com.ruoyi.operator.mapper.OptMemberOrderDetailMapper;
import com.ruoyi.operator.mapper.OptShopMemberMapper;
import com.ruoyi.operator.service.IOptMemberOrderDetailService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.operator.mapper.OptMemberOrderMapper;
import com.ruoyi.operator.domain.OptMemberOrder;
import com.ruoyi.operator.service.IOptMemberOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 会员收支记录Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-27
 */
@Service
public class OptMemberOrderServiceImpl implements IOptMemberOrderService {
    @Autowired
    private OptMemberOrderMapper optMemberOrderMapper;

    @Autowired
    private OptMemberOrderDetailMapper optMemberOrderDetailMapper;

    @Autowired
    private OptShopMemberMapper optShopMemberMapper;

    /**
     * 查询会员收支记录
     *
     * @param id 会员收支记录ID
     * @return 会员收支记录
     */
    @Override
    public OptMemberOrder selectOptMemberOrderById(Long id) {
        return optMemberOrderMapper.selectOptMemberOrderById(id);
    }

    /**
     * 查询会员收支记录列表
     *
     * @param optMemberOrder 会员收支记录
     * @return 会员收支记录
     */
    @Override
    public List<OptMemberOrder> selectOptMemberOrderList(OptMemberOrder optMemberOrder) {
        return optMemberOrderMapper.selectOptMemberOrderList(optMemberOrder);
    }

    /**
     * 查询商户订单数
     *
     * @param deptId 组织Id
     * @return 结果
     */
    public int selectOrderCountByDept(Long deptId){
        return optMemberOrderMapper.selectOrderCountByDept(deptId);
    }

    /**
     * 新增会员收支记录
     *
     * @param optMemberOrder 会员收支记录
     * @return 结果
     */
    @Override
    public int insertOptMemberOrder(OptMemberOrder optMemberOrder) {
        OptShopMember optshopmember = new OptShopMember();
        OptShopMember optMemberOrderinfo = optShopMemberMapper.selectMemberByCardNumber(optMemberOrder.getMemberCardNumber());
        if (null != optMemberOrderinfo && !optMemberOrderinfo.equals("")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
            String nowdate = sdf.format(new Date());
            String number = "C" + nowdate +RandomStringUtils.randomNumeric(6);
            OptMemberOrderDetail optMemberOrderDetail = new OptMemberOrderDetail();
            SysUser currentUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            if(null!=optMemberOrder.getGivingAmount()){
                optMemberOrder.setBalance(optMemberOrder.getConsumeAmount()
                        .add(optMemberOrder.getGivingAmount()).add(optMemberOrderinfo.getBalance()));
                optMemberOrderDetail.setGivingAmount(optMemberOrder.getGivingAmount());
                optMemberOrderDetail.setActivityBalance(optMemberOrderinfo.getActivityBalance()
                        .add(optMemberOrder.getGivingAmount()));
                optMemberOrderDetail.setBalance(optMemberOrderinfo.getActivityBalance().add(optMemberOrder.getGivingAmount()
                        .add(optMemberOrderinfo.getPrincipalBalance().add(optMemberOrder.getConsumeAmount()))));
                optshopmember.setActivityBalance(optMemberOrderinfo.getActivityBalance().add(optMemberOrder.getGivingAmount()));
                optshopmember.setPrincipalBalance(optMemberOrderinfo.getPrincipalBalance().add(optMemberOrder.getConsumeAmount()));
                optshopmember.setBalance(optMemberOrderinfo.getActivityBalance().add(optMemberOrder.getGivingAmount()
                        .add(optMemberOrderinfo.getPrincipalBalance().add(optMemberOrder.getConsumeAmount()))));
            }else{
                optMemberOrder.setBalance(optMemberOrder.getConsumeAmount().add(optMemberOrderinfo.getBalance()));
                optMemberOrderDetail.setActivityBalance(optMemberOrderinfo.getActivityBalance());
                optMemberOrderDetail.setBalance(optMemberOrderinfo.getActivityBalance()
                        .add(optMemberOrderinfo.getPrincipalBalance().add(optMemberOrder.getConsumeAmount())));
                optshopmember.setActivityBalance(optMemberOrderinfo.getActivityBalance());
                optshopmember.setPrincipalBalance(optMemberOrderinfo.getPrincipalBalance().add(optMemberOrder.getConsumeAmount()));
                optshopmember.setBalance(optMemberOrderinfo.getActivityBalance()
                        .add(optMemberOrderinfo.getPrincipalBalance().add(optMemberOrder.getConsumeAmount())));
            }
            optMemberOrder.setOrderId(number);
            optMemberOrder.setMemberCardNumber(optMemberOrder.getMemberCardNumber());
            optMemberOrder.setCreateTime(DateUtils.getNowDate());
            optMemberOrder.setUserName(optMemberOrderinfo.getUserName());
            optMemberOrder.setCreateBy(currentUser.getUserName());
            optMemberOrder.setDeptId(currentUser.getDept().getDeptId());
            optMemberOrder.setDeptName(currentUser.getDept().getDeptName());
            optMemberOrder.setMobile(optMemberOrderinfo.getMobile());
            optMemberOrder.setBehalfType("C");
            optMemberOrder.setNikname(optMemberOrderinfo.getNikname());
            optMemberOrder.setCreateUser(currentUser.getUserName());
            optMemberOrderDetail = new OptMemberOrderDetail();
            optMemberOrderDetail.setOrderId(number);
            optMemberOrderDetail.setBehalfType("C");
            optMemberOrderDetail.setDeptId(currentUser.getDept().getDeptId());
            optMemberOrderDetail.setDeptName(currentUser.getDept().getDeptName());
            optMemberOrderDetail.setMemberCardNumber(optMemberOrder.getMemberCardNumber());
            optMemberOrderDetail.setBehalfType(optMemberOrder.getBehalfType());
            optMemberOrderDetail.setRechargeAmount(optMemberOrder.getConsumeAmount());
            optMemberOrderDetail.setNikname(optMemberOrder.getNikname());
            optMemberOrderDetail.setGivingAmount(optMemberOrder.getGivingAmount());
            optMemberOrderDetail.setPrincipalBalance(optMemberOrder.getConsumeAmount());
            optMemberOrderDetail.setCreateUser(currentUser.getUserName());
            optMemberOrderDetail.setCreateTime(DateUtils.getNowDate());
            optMemberOrderDetailMapper.insertOptMemberOrderDetail(optMemberOrderDetail);
            optshopmember.setMemberCardNumber(optMemberOrderinfo.getMemberCardNumber());
            optShopMemberMapper.updateOptShopMemberBalance(optshopmember);
        }
        return optMemberOrderMapper.insertOptMemberOrder(optMemberOrder);
    }




    @Override
    public int insertOptMemberOrderExpense(OptMemberOrder optMemberOrder) {
        OptShopMember optshopmember = new OptShopMember();
        OptShopMember optMemberOrderinfo = optShopMemberMapper.selectMemberByCardNumber(optMemberOrder.getMemberCardNumber());
        if (null != optMemberOrderinfo && !optMemberOrderinfo.equals("")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
            String nowdate = sdf.format(new Date());
                String number = "X" + nowdate +RandomStringUtils.randomNumeric(6);
            optMemberOrder.setMemberCardNumber(optMemberOrder.getMemberCardNumber());
//            OptMemberOrder optMemberOrderdetil = optMemberOrderMapper.selectMemberCardNumberOder(optMemberOrder);
            OptMemberOrderDetail optMemberOrderDetail = new OptMemberOrderDetail();
            SysUser currentUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            optMemberOrder.setOrderId(number);
            optMemberOrder.setCreateTime(DateUtils.getNowDate());
            optMemberOrder.setUserName(optMemberOrderinfo.getUserName());
            optMemberOrder.setCreateBy(currentUser.getUserName());
            optMemberOrder.setDeptId(currentUser.getDept().getDeptId());
            optMemberOrder.setDeptName(currentUser.getDept().getDeptName());
            optMemberOrder.setMobile(optMemberOrderinfo.getMobile());
            optMemberOrder.setBehalfType("X");
            optMemberOrder.setNikname(optMemberOrderinfo.getNikname());
            optMemberOrder.setCreateUser(currentUser.getUserName());
            optMemberOrderDetail = new OptMemberOrderDetail();
            optMemberOrderDetail.setBehalfType("X");
            optMemberOrderDetail.setOrderId(optMemberOrder.getOrderId());
            BigDecimal principalBalance = null;
            BigDecimal balance = null;
            BigDecimal difference = null;
            optMemberOrderDetail.setMemberCardNumber(optMemberOrderinfo.getMemberCardNumber());
            if (optMemberOrder.getConsumeAmount().compareTo(optMemberOrderinfo.getPrincipalBalance()) == 1) {
                difference =  optMemberOrder.getConsumeAmount().subtract(optMemberOrderinfo.getPrincipalBalance());
                principalBalance = BigDecimal.ZERO;
                balance =optMemberOrderinfo.getBalance()
                        .subtract(optMemberOrder.getConsumeAmount());
                optMemberOrderDetail.setPrincipalBalance(principalBalance);
                optMemberOrderDetail.setBalance(balance);
//                optMemberOrderDetail.setRechargeAmount(BigDecimal.ZERO);
                optMemberOrderDetail.setActivityBalance(optMemberOrderinfo.getActivityBalance()
                        .subtract(difference));
                optshopmember.setBalance(balance);
                optMemberOrder.setGivingAmount(optMemberOrderinfo.getActivityBalance()
                        .subtract(difference));
                optshopmember.setMemberCardNumber(optMemberOrderinfo.getMemberCardNumber());
                optshopmember.setPrincipalBalance(principalBalance);
                optshopmember.setActivityBalance(optMemberOrderinfo.getActivityBalance().subtract(difference));
                optShopMemberMapper.updateOptShopMemberBalance(optshopmember);
                optMemberOrder.setBalance(balance);
            } else {
                principalBalance = optMemberOrderinfo.getPrincipalBalance().subtract(optMemberOrder.getConsumeAmount());
                balance = optMemberOrderinfo.getPrincipalBalance().add(optMemberOrderinfo.getActivityBalance())
                        .subtract(optMemberOrder.getConsumeAmount());
                optMemberOrderDetail.setPrincipalBalance(principalBalance);
                optMemberOrderDetail.setBalance(balance);
//                optMemberOrderDetail.setRechargeAmount(optMemberOrderinfo.getPrincipalBalance()
//                        .subtract(optMemberOrder.getConsumeAmount()));
                optMemberOrderDetail.setActivityBalance(optMemberOrderinfo.getActivityBalance());
//                optMemberOrderDetail.setGivingAmount(optMemberOrderdetil.getGivingAmount());
                optshopmember.setBalance(balance);
                optshopmember.setMemberCardNumber(optMemberOrderinfo.getMemberCardNumber());
                optshopmember.setPrincipalBalance(principalBalance);
                optshopmember.setActivityBalance(optMemberOrderinfo.getActivityBalance());
                optMemberOrder.setBalance(balance);
                optMemberOrder.setGivingAmount(optMemberOrderinfo.getActivityBalance());
                optShopMemberMapper.updateOptShopMemberBalance(optshopmember);
            }
            optMemberOrderDetail.setCreateTime(DateUtils.getNowDate());
            optMemberOrderDetail.setDeptId(currentUser.getDept().getDeptId());
            optMemberOrderDetail.setDeptName(currentUser.getDept().getDeptName());
            optMemberOrderDetail.setConsumeAmount(optMemberOrder.getConsumeAmount());
            optMemberOrderDetail.setNikname(optMemberOrderinfo.getNikname());
            optMemberOrderDetail.setCreateUser(currentUser.getUserName());
            optMemberOrderDetailMapper.insertOptMemberOrderDetail(optMemberOrderDetail);
        }
        return optMemberOrderMapper.insertOptMemberOrder(optMemberOrder);
    }
}
